//
//  PageTitleView.swift
//  DouYuZB
//
//  Created by DSDL on 2016/10/25.
//  Copyright © 2016年 鼎商动力. All rights reserved.
//

import UIKit
//MARK: - 定义协议
///title被点击的代理
protocol PageTitleViewDelegate: class {
    func pageTitleView(_ title: PageTitleView, selectedIndex index: Int)
}
//MARK: - 定义常量
private let kScrollLineH: CGFloat = 2
private let kNormalColor: (CGFloat,CGFloat,CGFloat) = (85,85,85)
private let kSelectColor: (CGFloat,CGFloat,CGFloat) = (255,128,0)
//MARK: - 定义 PageTitleView 类
class PageTitleView: UIView {
    
    //MARK: - 定义属性
    fileprivate var currentIndex: Int = 0
    fileprivate var titlTextArr: [String]?
    weak var delegate: PageTitleViewDelegate?
    //MARK: - 懒加载属性
    fileprivate lazy var titleLabelArr = [UILabel]()
    
    /*创建scrolerView**/
    fileprivate lazy var scrollView: UIScrollView = {
        let scrollView  = UIScrollView()
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.scrollsToTop = false
        scrollView.bounces = false
        return scrollView
    }()
    
    /*创建 scrollerLine  滑动条**/
    fileprivate lazy var scrollLine: UIView = {
        let scrollLine = UIView()
        scrollLine.backgroundColor = UIColor.orange
        return scrollLine
    }()
    
    //MAKR: - 自定义构造函数
    init(frame: CGRect, title: [String]) {
        self.titlTextArr  = title
 
        super.init(frame: frame)
        //设置UI
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
//MARK: - 设置UI
extension PageTitleView {
    
    fileprivate func setupUI() {
        //1.添加 scrollView
        addSubview(scrollView)
        scrollView.frame = bounds
        
        //2.添加title 对应的label
        setupTitleLabels()
        
        //3.设置底线
        setupBottomLineAndScrollLine()
    }
    fileprivate func setupTitleLabels() {
        guard let titles = titlTextArr else{return}
        
        //1.先确定label 的一些frame值
        let  labelW: CGFloat = frame.width / CGFloat(titles.count)
        let  labelH: CGFloat = frame.height - kScrollLineH
        let  labelY: CGFloat = 0
        
        for (index, title) in titles.enumerated(){
            //2.创建 label
            let label = UILabel()
            label.text = title
            label.tag = index
            label.font = UIFont.systemFont(ofSize: 16.0)
            label.textColor = UIColor(r: kNormalColor.0, g: kNormalColor.1, b: kNormalColor.2)
            label.textAlignment = .center
            
            //3.设置labeld的frame1
            let  labelX: CGFloat =  labelW * CGFloat(index)
            label.frame = CGRect(x: labelX, y: labelY, width:labelW, height: labelH)
            
            //4.添加label 到scrollerView
            addSubview(label)
            titleLabelArr.append(label)
            //5.给label添加手势
            label.isUserInteractionEnabled = true
            let tapGes = UITapGestureRecognizer(target: self, action: #selector(titleLabelClick(_:)))
            label.addGestureRecognizer(tapGes)
        }
    }
    fileprivate func setupBottomLineAndScrollLine(){
        //1.添加底线
        let bottomline = UIView()
        bottomline.backgroundColor = UIColor.lightGray
        
        let lineH: CGFloat = 0.5
        bottomline.frame = CGRect(x: 0, y: frame.height - lineH, width: frame.width, height: lineH)
        addSubview(bottomline)
        
        //2.添加scrollLine 滑动条
        //获取第一个Label
        guard let firstlabel = titleLabelArr.first else{return}
        firstlabel.textColor = UIColor(r: kSelectColor.0, g: kSelectColor.1, b: kSelectColor.2)
        
        scrollView.addSubview(scrollLine)
        scrollLine.frame = CGRect(x: firstlabel.frame.origin.x, y: frame.height - kScrollLineH, width: firstlabel.frame.width, height: kScrollLineH)
        
    }
}
//MARK: - 监听label的点击
extension  PageTitleView {
    @objc fileprivate func titleLabelClick(_ tapGes: UITapGestureRecognizer){
       
        //0.获取当前label
        guard  let  currentLabel = tapGes.view as? UILabel else { return }
         //1.如果是重复点击的title 那么直接返回
        if currentLabel.tag == currentIndex { return }
        //2.获取之前的labl
        let oldLabel = titleLabelArr [currentIndex]
        //3.切换文字的颜色
        currentLabel.textColor = UIColor(r: kSelectColor.0, g: kSelectColor.1, b: kSelectColor.2)
        oldLabel.textColor  = UIColor(r: kNormalColor.0, g: kNormalColor.1, b: kNormalColor.2)
        //4.保存最新label的tag值
        currentIndex = currentLabel.tag
        
        //5.滚动条发生改变
        let scrollLineX = CGFloat(currentLabel.tag) * scrollLine.frame.width
        UIView.animate(withDuration: 0.3, animations: { [weak self] in
            self?.scrollLine.frame.origin.x = scrollLineX
        }) 
        //6.通知代理
        delegate?.pageTitleView(self, selectedIndex: currentIndex)
        
    }
}
//MARK: - 对外暴露的方法
extension PageTitleView {
    
    //渐变色
    func setTitltWithProgress(_ progress: CGFloat, sourceIndex: Int, targetIndex: Int) {
        //1.拿到sourceLabel / targetLabel
        let sourceLabel = titleLabelArr[sourceIndex]
        let targetLabel = titleLabelArr[targetIndex]
        
        //2.处理滑块的逻辑
        let moveTotalX = targetLabel.frame.origin.x - sourceLabel.frame.origin.x
        let moveX = moveTotalX * progress
        scrollLine.frame.origin.x = sourceLabel.frame.origin.x + moveX
        
        //3.文字的渐变颜色(复杂)
        //3.1 取出变化的范围
        let colorDelta = (kSelectColor.0 - kNormalColor.0,
                          kSelectColor.1 - kNormalColor.1,
                          kSelectColor.2 - kNormalColor.2)
        //3.2 变化的sourcelabel
        sourceLabel.textColor = UIColor(r: kSelectColor.0 - colorDelta.0 * progress,
                                        g: kSelectColor.1 - colorDelta.1 * progress,
                                        b: kSelectColor.2 - colorDelta.2 * progress)
         //3.2 变化的targetLabel
        targetLabel.textColor = UIColor(r: kNormalColor.0 + colorDelta.0 * progress,
                                        g: kNormalColor.1 + colorDelta.1 * progress,
                                        b: kNormalColor.2 + colorDelta.2 * progress)
        //4.记录最新的index
        currentIndex = targetIndex
    }
}
